#include "statemanager.h"

#include <gui/state.h>
#include <gui/paintmanager.h>

StateManager::StateManager(QObject *parent) :
    QObject(parent)
{
    myPaintManager = new PaintManager(this);
}

void StateManager::pushState(State *state) {
    if(myStates.size() > 0) {
        myStates[myStates.size() - 1]->covered();
    }
    myStates.append(state);
    state->added();

    myPaintManager->setScene(state->getScene());

    //disconnect()
    connect(state, SIGNAL(updated()), this, SIGNAL(updated()));
}

State *StateManager::popState() {
    State *state = 0;
    if(myStates.size() > 0) {
        state = myStates[myStates.size() - 1];
        state->deleted();
        myStates.remove(myStates.size() - 1);
        if(myStates.size() > 0) {
            myStates[myStates.size() - 1]->returned();
            myPaintManager->setScene(myStates[myStates.size() - 1]->getScene());
        }
    }

    return state;
}

State *StateManager::curState() {
    return myStates[myStates.size() - 1];
}

PaintManager *StateManager::getPaintManager() const {
    return myPaintManager;
}
